<?php
namespace Core\Controller;


use Core\Service\Mail\MailInterface;
use Model\Factory\TableFinder;

use Model\Factory\TableGatewayFactory;
use Model\TableGatewayTransactionProxy;
use Zend\EventManager\EventManager;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\MvcEvent;
use Core\Service\Digest\DigestServiceInterface;

class BaseController extends  AbstractActionController{

    /**
     * @var DigestServiceInterface
     */
    protected $digestService;


    /**
     * @var MailInterface
     */
    protected $mailService;

    /**
     * @return \Core\Service\Digest\DigestServiceInterface
     */
    public function getDigestService()
    {
        if(!$this->digestService)
            $this->digestService = $this->getServiceLocator()->
                get('DigestService');
        return $this->digestService;
    }


    /**
     * @return \Core\Service\Mail\MailInterface
     */
    public function getMailService()
    {
        if(!$this->mailService)
            $this->mailService = $this->getServiceLocator()->
                get('MailService');
        return $this->mailService;
    }

    /**
     * @var TableFinder
     */
    protected $tableFinder;

    /**
     * @var TableGatewayTransactionProxy
     */
    protected $tableGatewayTransactionProxy;

    /**
     * @return TableFinder
     */
    public function getTableFinder()
    {
        if(!$this->tableFinder)
            $this->tableFinder = $this->getServiceLocator()->
                get('TableFinder');
        return $this->tableFinder;
    }


    /**
     * @return TableGatewayTransactionProxy
     */
    public function getTableGatewayTransactionProxy()
    {
        if(!$this->tableGatewayTransactionProxy)
            $this->tableGatewayTransactionProxy = $this->getServiceLocator()->
                get('TableGatewayTransactionProxy');
        return $this->tableGatewayTransactionProxy;
    }


    public function onDispatch(MvcEvent $e)
    {
        //------do some common stuff here--------------
        $this->events->addIdentifiers('Core\Controller\BaseController');
        TableGatewayFactory::setDbAdapter($this->getServiceLocator()->
            get('Zend\Db\Adapter\Adapter'));
        return parent::onDispatch($e);
    }


    protected function fireExceptionEvent($ex, $eventName){
        $this->getEventManager()->trigger(
            $eventName ,$this, array('exception'=>$ex));
    }
}
